package com.itheima.demo;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;


/**
 * @author Zhang_Han
 * @version 1.0
 * @date 2020/7/10 18:19
 */
public class Demo_001 {
    public static void main(String[] args) throws IOException {
        //对文件中的数据按照年龄的从大到小的顺序进行排序
        //先读取文件中的元素,创建字符输入流;
        BufferedReader br = new BufferedReader(new FileReader("demo_IO\\name.txt"));
        //读取文件中的元素;每次读取一行;
        ArrayList<Student> list = new ArrayList<>();
        String line;
        while ((line = br.readLine()) != null) {
            //将读取到的元素切割存储到集合当中;
            String[] split = line.split(",");
            Student stu = new Student(split[0], Integer.parseInt(split[1]));
            list.add(stu);
        }
        br.close();
        //System.out.println(list);
        //将读取出来的元素进行排序;
        Collections.sort(list, (stu1, stu2) -> stu2.getAge() - stu1.getAge());
        //创建字符输入流;
        BufferedWriter bw = new BufferedWriter(new FileWriter("demo_IO\\name.txt"));
        for (Student student : list) {
            bw.write(new String(student.getName() + "," + student.getAge()));
            bw.newLine();
        }
        bw.close();
    }
}
